/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.gui.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import kameleon.exception.FileReadingException;
import kameleon.gui.exception.UnknownKeyException;
import kameleon.gui.model.FileModel;
import kameleon.gui.model.Message;
import kameleon.gui.model.Model;
import kameleon.gui.model.PlugInMessage;
import kameleon.gui.model.PlugInMessage.State;
import kameleon.gui.util.FileConstants;
import kameleon.gui.util.ImageUtility;

/**
 * View displaying a simple information message about adde or removed plug-ins.
 * The message is displayed with an icon to its left matching the type of message : 
 * error, success, adding an analyzer or a generator.
 * 
 * @author		Schnell Michaël
 * @version		1.0
 */
public class ViewPlugInMessage extends ViewMessage 
implements FileConstants {

	/**
	 * Needed to serialize this class.
	 * 
	 * @see		java.io.Serializable
	 */
	private static final long serialVersionUID = -315973344403420113L ;

	/**
	 * Graphical component displaying the text of the message.
	 */
	private JLabel messageLabel ;

	/**
	 * Icon displayed left of the text.
	 */
	private JLabel icon ;

	/**
	 * Layout manager for this view.
	 */
	private GridBagLayout gridbag ;

	/**
	 * Sole constructor.
	 * 
	 * @param 	model
	 * 			model used by this view
	 * 
	 * @param 	pMessage
	 * 			model backing the information about the displayed message
	 * 
	 * @throws 	FileReadingException
	 * 			If an icon could not be loaded  
	 * 
	 * @throws 	UnknownKeyException
	 * 			if an error occurred while updating the text of this view
	 */
	public ViewPlugInMessage(Model model, PlugInMessage pMessage)
			 throws FileReadingException, UnknownKeyException {
		this(new GridBagLayout(), model, pMessage) ;
	}// ViewPlugInMessage(Model, PlugInMessage)

	/**
	 * Sole constructor.
	 * 
	 * @param	gridbag
	 * 			layout manager uesd by this instance
	 * 
	 * @param 	model
	 * 			model used by this view
	 * 
	 * @param 	pMessage
	 * 			model backing the information about the displayed message
	 * 
	 * @throws 	FileReadingException
	 * 			If an icon could not be loaded  
	 * 
	 * @throws 	UnknownKeyException
	 * 			if an error occurred while updating the text of this view
	 */
	private ViewPlugInMessage(GridBagLayout gridbag, Model model, 
			PlugInMessage pMessage) throws FileReadingException, UnknownKeyException {
		super(model, pMessage) ;
		this.gridbag = gridbag ;
		this.setLayout(this.gridbag) ;
		
		this.setBorder(BorderFactory.createTitledBorder(
				FileModel.DATE_FORMATTER.format(new Date()))) ;

		this.build(pMessage) ;
		this.reloadLanguage() ;
	}// ViewPlugInMessage(GridBagLayout, Model, PlugInMessage)

	/**
	 * Builds the content of this instance.
	 * 
	 * @param 	pMessage
	 * 			model backing the information about the displayed information
	 * 
	 * @throws 	FileReadingException
	 * 			If an icon could not be loaded  
	 */
	private void build(PlugInMessage pMessage) throws FileReadingException {
		this.makeMessageRow(pMessage) ;
	}// build(PlugInMessage)

	/**
	 * Builds the components need to display the message with the appropriate icon.
	 * 
	 * @param	pMessage
	 * 			model backing the information about the displayed information
	 * 
	 * @throws 	FileReadingException
	 * 			If an icon could not be loaded 
	 */
	private void makeMessageRow(PlugInMessage pMessage) throws FileReadingException {
		this.makeIcon(pMessage.getState()) ;
		this.makeLabel(pMessage) ;
	}// makeMessageRow(PlugInMessage)

	/**
	 * Builds the appropriate icon for the given message.
	 * 
	 * @param 	type
	 * 			type of the displayed information
	 * 
	 * @throws 	FileReadingException
	 * 			If an icon could not be loaded  
	 */
	private void makeIcon(State type) throws FileReadingException {
		JLabel label = null ;
		GridBagConstraints constraints = new GridBagConstraints() ;

		label = new JLabel() ;
		this.initIcon(label, type) ;

		constraints.gridwidth = 1 ;
		constraints.anchor = GridBagConstraints.ABOVE_BASELINE_TRAILING ;
		constraints.fill = GridBagConstraints.HORIZONTAL ;
		constraints.insets = new Insets(2, 2, 2, 2) ;
		constraints.weightx = 0.0 ;
		constraints.weighty = 0.0 ;
		this.add(label) ;
		this.gridbag.setConstraints(label, constraints) ;

		this.icon = label ;
	}// makeIcon(State)
	
	/**
	 * Builds the component used to display the text of the message.
	 * 
	 * @param	pMessage
	 * 			model backing the information about the displayed information
	 */
	private void makeLabel(PlugInMessage pMessage) {
		GridBagConstraints constraints = new GridBagConstraints() ;
		
		this.messageLabel = new JLabel() ;
		constraints.gridwidth = GridBagConstraints.REMAINDER ;
		constraints.anchor = GridBagConstraints.ABOVE_BASELINE_LEADING ;
		constraints.fill = GridBagConstraints.HORIZONTAL ;
		constraints.weightx = 1.0 ;
		constraints.weighty = 0.0 ;
		
		this.add(this.messageLabel) ;
		this.gridbag.setConstraints(this.messageLabel, constraints) ;
	}// makeLabel(PlugInMessage)

	/**
	 * Initializes the icon of the given label using the
	 * given state.
	 * 
	 * @param 	iconLabel
	 * 			label that will contain the icon
	 * 
	 * @param 	type
	 * 			type used to determine the matching icon
	 * 
	 * @throws 	FileReadingException
	 * 			If an icon could not be loaded  
	 */
	private void initIcon(JLabel iconLabel, State type)
			throws FileReadingException {
		String iconPath = null ;

		switch(type) {
		case ADDING_PLUGIN:
			iconPath = ADD_PLUGIN_FILE ;
			break ;
			
		case REMOVING_PLUGIN:
			iconPath = REMOVE_PLUGIN_FILE ;
			break ;

		case SUCCESS:
			iconPath = CONVERTED_FILE ;
			break ;

		case ERROR:
			iconPath = ERROR_FILE ;
		}// switch

		InputStream src = new BufferedInputStream(ViewInformationMessage.class
				.getResourceAsStream(iconPath)) ;
		iconLabel.setIcon(new ImageIcon(ImageUtility.getImageBytes(src))) ;
		try {
			src.close() ;
		} catch(IOException ioe) {
			this.model.displayDebugInformation(
					new FileReadingException(ioe.getMessage())) ;
		}// try
	}// initIcon(JLabel, State)

	/**
	 * Updates the displayed text.
	 * 
	 * @param	newMessage
	 * 			new message informations
	 * 
	 * @throws 	FileReadingException
	 * 			If an icon could not be loaded  
	 * 
	 * @throws 	UnknownKeyException
	 * 			if an error occurred while updating the text of this view
	 */
	@Override
	public void updateMessage(Message newMessage) 
			throws FileReadingException, UnknownKeyException {
		this.message = newMessage ;
		PlugInMessage pMessage = (PlugInMessage) newMessage ;
		this.initIcon(this.icon, pMessage.getState()) ;
		// Update message
		this.reloadLanguage() ;
	}// updateMessage(Message)
	
	/** {@inheritDoc} */
	@Override
	public void reloadLanguage() throws UnknownKeyException {
		PlugInMessage pMessage = (PlugInMessage) this.message ;
		this.messageLabel.setText(pMessage.getMessage()) ;
	}// reloadLanguage()

	/**	{@inheritDoc} */
	@Override
	public void update() {
		/* Nothing to be done.*/
	}// update()

}// class ViewPlugInMessage